<?php

/**
 * SettingService.class.php 
 * 
 *
 * @author     late.xiao@qq.com
 */
namespace System\Service;

use Base\Lib\BaseService;

class SettingService extends BaseService {
    
    public function addData($request)
    {
        $data = $this->proKeyVal($request);
        $obj_mdl = D('setting');
        
        return $obj_mdl->addAll($data);
    }
    
    public function updateData($request)
    {
        $data = $this->proKeyVal($request, false);
        $obj_mdl = D('setting');
        try {
            $obj_mdl->startTrans();
            foreach ($data as $val)
            {
                $where['s_key'] = $val['s_key'];
                unset($val['s_key']);
                $obj_mdl->where($where)->save($val);
            }
            $obj_mdl->commit();
        } catch (\Exception $e) {
            $obj_mdl->rollback();
            throw $e;
            return false;
        }
        
        return true;
    }
    
    public function getSetting()
    {
        $obj_mdl = D('setting');
        $result = $obj_mdl->select();
        if(!$result)
        {
            return false;
        }
        
        $rs = array();
        foreach ($result as $val)
        {
            if($val['s_type'] == 1)
            {
                $val['s_value'] = unserialize($val['s_value']);
            }
            $rs[$val['s_key']] = $val['s_value'];
        }
        
        return $rs;
    }
    
    public function getRow($filter)
    {
        $obj_mdl = D('setting');
        $result = $obj_mdl->where($filter)->find();
        if(!$result)
        {
            return false;
        }
        
        if($result['s_type'] == 1)
        {
            $result['s_value'] = unserialize($result['s_value']);
        }

        return $result['s_value'];
    }
    
    
}
 